package cn.edu360.day06;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Client {
	final static String KEY = "天王盖地虎";

	public static void main(String[] args) {
		String path = "d:\\wc2.jar";
		String comd = "java -jar d:\\wc2.jar";

		try {
			// 1.建立通信
			Socket socket = new Socket("localhost", 8888);
			// 2.发送身份验证信息
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(KEY);
			oos.flush();
			// 3.接受信息并返回给服务器端信息
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			String value = (String) ois.readObject();
			if ("宝塔镇河妖".equals(value)) {
				oos.writeObject(path);
				oos.flush();
				// 发送jar文件
				FileInputStream fis = new FileInputStream("d:\\wc.jar");
				byte[] b = new byte[1024];
				int line = 0;
				while ((line = fis.read(b)) != -1) {
					oos.write(b, 0, line);
				}
				oos.flush();
				fis.close();
				// 发送comd
				oos.writeObject(comd);
				oos.flush();
				// 接受jar文件
				value = (String) ois.readObject();
			} else {
				System.out.println("身份验证失败.....");
			}
			socket.shutdownInput();
			socket.shutdownOutput();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
